import 'package:airbnb_clone/components/shadow_widget.dart';
import 'package:airbnb_clone/constant/constant.dart';
import 'package:airbnb_clone/models/user_message.dart';
import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';

// ignore: must_be_immutable
class ChatBody extends StatefulWidget {
  List<UserMessage> messageList;
  int friendId;
  int selfId;
  ChatBody(
      {required this.messageList,
      required this.selfId,
      required this.friendId,
      super.key});

  @override
  State<ChatBody> createState() => _ChatBodyState();
}

class _ChatBodyState extends State<ChatBody> {
  @override
  Widget build(BuildContext context) {
    return Container(
      // padding: EdgeInsets.symmetric(horizontal: 14),
      padding: const EdgeInsets.only(left: 14),
      child: ListView.builder(
        padding: EdgeInsets.zero,
        itemCount: widget.messageList.length,
        itemBuilder: (context, index) {
          return Container(
            margin:const EdgeInsets.only(bottom: 8),
            child: ConstrainedBox(
                constraints: const BoxConstraints(maxHeight: 500),
                child: SwitchWidget(context, index)),
          );
        },
      ),
    );
  }

  // 切换组件
  // ignore: non_constant_identifier_names
  Widget SwitchWidget(BuildContext context, int index) {
    if (widget.messageList[index].receptorId == widget.friendId) {
      return RowSelf(context, index)
          .animate()
          .fadeIn(duration: 600.ms, delay: 200.ms)
          .move(begin: const Offset(0, -30), curve: Curves.easeOutQuad);
    } else if (widget.messageList[index].receptorId == -1) {
      return RowNotice(context, index)
          .animate()
          .fadeIn(duration: 600.ms, delay: 200.ms);
    } else if (widget.messageList[index].senderId == widget.selfId) {
      return RowFriend(context, index)
          .animate()
          .fadeIn(duration: 600.ms, delay: 200.ms)
          .move(begin: const Offset(0, 30), curve: Curves.easeOutQuad);
      
    } else {
      return RowNotice(context, index);
    }
  }

  // 别人
  // ignore: non_constant_identifier_names
  Row RowFriend(BuildContext context, int index) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // 头像
        Container(
            width: 60,
            height: 60,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(12),
            ),
            child: ShadowWidget(
                originWidget: ClipRRect(
              borderRadius: BorderRadius.circular(12),
              child: Image.network(
                widget.messageList[index].userImg,
                errorBuilder: (context, error, stackTrace) {
                  return Image.asset("assets/images/netError.jpg");
                },
              ),
            ))),
        // 消息框
        const SizedBox(
          width: 10,
        ),
        Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              " ${widget.messageList[index].userName}",
              style: titleTextStyle,
            ),
            ConstrainedBox(
              constraints:const BoxConstraints(maxWidth: 250.0, maxHeight: 500),
              child: Container(
                padding:const EdgeInsets.all(8),
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(8)),
                child: Text(
                  widget.messageList[index].context,
                  softWrap: true,
                  style: normalTextStyle,
                  maxLines: 20,
                  overflow: TextOverflow.ellipsis,
                ),
              ),
            ),
            Text(
              "${widget.messageList[index].createTime.year}-${widget.messageList[index].createTime.month}-${widget.messageList[index].createTime.day}",
              style: normalTextStyle,
            )
          ],
        )
      ],
    );
  }

  // 自已
  // ignore: non_constant_identifier_names
  Row RowSelf(BuildContext context, int index) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.end,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // 消息框
        Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            Text(
              " ${widget.messageList[index].userName}",
              style: titleTextStyle,
            ),
            ConstrainedBox(
              constraints: const BoxConstraints(
                maxWidth: 250.0,
              ),
              child: Container(
                padding: const EdgeInsets.all(8),
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(8)),
                child: Text(
                  widget.messageList[index].context,
                  softWrap: true,
                  style: normalTextStyle,
                  maxLines: 20,
                  overflow: TextOverflow.ellipsis,
                ),
              ),
            ),
            Text(
              "${widget.messageList[index].createTime.year}-${widget.messageList[index].createTime.month}-${widget.messageList[index].createTime.day}",
              style: normalTextStyle,
            )
          ],
        ),
        // 头像
        const SizedBox(
          width: 10,
        ),
        Container(
            width: 60,
            height: 60,
            margin: const EdgeInsets.only(right: 20),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(12),
            ),
            child: ShadowWidget(
              originWidget: ClipRRect(
                borderRadius: BorderRadius.circular(12),
                child: Image.network(
                  widget.messageList[index].userImg,
                  errorBuilder: (context, error, stackTrace) {
                    return Image.asset("assets/images/netError.jpg");
                  },
                ),
              ),
            )),
      ],
    );
  }

  // 官方
  // ignore: non_constant_identifier_names
  Widget RowNotice(BuildContext context, int index) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Container(
          padding: const EdgeInsets.all(14),
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(8),
              color: Colors.black.withOpacity(0.5)),
          child: Text(
            "Msg:${widget.messageList[index].context}",
            style: const TextStyle(color: Colors.white),
          ),
        )
      ],
    );
  }
}
